<template>
  <el-card class="box-card">
    <div slot="header" class="clearfix">
      <span>商品添加</span>
    </div>

    <el-form ref="form" :model="form" label-width="80px">
      <el-form-item label="商品名称">
        <el-input v-model="form.name"></el-input>
      </el-form-item>

      <el-form-item label="商品分类">
        <el-select v-model="form.category" placeholder="请选择活动区域">
          <el-option label="分类一" value="分类一"></el-option>
          <el-option label="分类二" value="分类二"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="商品价格">
        <el-input-number v-model="form.price" @change="handleChange" :min="1" :max="10" label="描述文字"></el-input-number>
      </el-form-item>

      <el-form-item label="商品图片">
        <el-upload :action="`${API}/goods/goods_img_upload`" list-type="picture-card"
        :on-success="handleAvatarSuccess"  
        :on-preview="handlePictureCardPreview" :on-remove="handleRemove">
          <i class="el-icon-plus"></i>
        </el-upload>
        <el-dialog :visible.sync="dialogVisible">
          <img width="100%" :src="dialogImageUrl" alt="">
        </el-dialog>

      </el-form-item>


      <el-form-item label="商品描述">
        <el-input type="textarea" v-model="form.goodsDesc"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="onSubmit">添加商品</el-button>
     
      </el-form-item>
    </el-form>

  </el-card>

</template>

<script>
import { updataGoodsReq } from '@/api/user';

export default {
  data() {
    return {
      dialogImageUrl: '',
      dialogVisible: false,
      API: 'http://sell.h5.itsource.cn:8087',
      
      form: {
        price: 1,
        name: '',
        category: '',
        imgUrl:'',
        goodsDesc: ''
      },

    }
  },
  methods: {
   async onSubmit() {
      // console.log(val);
      console.log('submit!');
      console.log(this.form);
      let res=await updataGoodsReq(this.form)
      console.log(res.data);
      let {code,msg}=res.data
      if(code===0){
        this.$router.push('/goods/goods-list')
      }

    },
    handleChange(value) {
      console.log(value);
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    handleAvatarSuccess(res){
      console.log(res);
      let {code,imgUrl}=res
      this.form.imgUrl=imgUrl

    }
  }
}
</script>

<style lang="scss" scoped>

</style>
